define(function(require,exports,module){
    var LayoutController=require("LayoutController");
    var Nav=function(options){
        this.init(options);
    }
    Nav.prototype.init=function(options){
        var self=this;
        this.options=$.extend(self.getDefaultOptions(),options);
        this.element=$("#nav").get(0);
        this.aList=$(this.element).find("a");
        for(var i=0;i<this.aList.length;i++){
            var aele=this.aList.eq(i);
            if(!!aele.next("ul")&&aele.next("ul").hasClass("open")){
                aele.data("click",true);
            }

        }
        this.aList.bind("click",function(event){
            return self.clickHandle(event);
        })
        $("i").not("#switchDiv").bind("click",function(event){
            event.stopPropagation();
            $(this).parent("a").click();
            return false;
        })
        var layoutController=new LayoutController();
    }
    Nav.prototype.getDefaultOptions=function(){
        var options={
            onlyOneOpen:false,//是否只有一条树开关
        }
        return options;
    }
    Nav.prototype.changeActive=function(ele){
        $(this.element).find("a.active").removeClass("active")
        ele.addClass("active")
    }
    Nav.prototype.clickHandle=function(event){

        var self=this;
        var ele=$(event.target);
        if(self.isDeeppestEle(ele)){
            self.changeActive(ele);
            return true;
        }
        if(ele.data("click")==true){
            self.switchAction(ele,false,self.options.onlyOneOpen);
            return false;
        }
        if(self.options.onlyOneOpen)self.closeAll();
        ele.data("click",true);
        if(ele.next("ul").find("li").length>0){
            self.switchAction(ele,true,self.options.onlyOneOpen);
            return false;
        }
        return false;
    }
    Nav.prototype.isDeeppestEle=function(ele){
        var a=!ele.next("ul").find("li").length>0;
        console.log(a);
        return a;
    }
    Nav.prototype.closeAll=function(){
        var self=this;
        for(var i=0;i<self.aList.length;i++){
            if(self.aList.eq(i).data("click")==true){
                self.switchAction(self.aList.eq(i),false)
            }
        }
    }
    Nav.prototype.switchAction=function(ele,bol,deep){
        var self=this;
        if(bol){
            ele.data("click",true);
            ele.next("ul").addClass("open");
            if(ele.parent("li").parent("ul").hasClass("sub-menu")&&deep){
                self.switchAction(ele.parent("li").parent("ul").prev("a"),true,true)
            }
        }else{
            ele.data("click",false);
            ele.next("ul").removeClass("open");
            if(deep&&ele.next("ul")){
                try {
                    for (var i = 0; i < ele.next("ul").children("li").length; i++) {
                        self.switchAction(ele.next("ul").children("li").children("a").eq(0), false, true)
                    }
                } catch (e) {
                    console.log(e);
                }
            }
        }
    }
    /*Nav.prototype.recursionTree=function(obj){

        return this.recursionTree(newObj,fun())
    }*/
    var nav=new Nav();
    module.exports=Nav;
})